﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace WodLib.Tools
{
    /// <summary>
    /// Object used to search types hierarchicaly in WodLib assembly
    /// </summary>
    public class HierarchicalTypeFinder
    {
        /// <summary>
        /// List of types found.
        /// </summary>
        public List<Type> TypesFound { get; set; }
        /// <summary>
        /// List of names of found types.
        /// </summary>
        public List<string> NamesFound { get; set; }

        /// <summary>
        /// Looks for types which are baseType in hierarchy.
        /// </summary>
        /// <param name="baseType">Type which subtybes and self look for.</param>
        public void FindTypes(Type baseType)
        {
            Assembly assm = Assembly.Load("WodLib");
            Type[] types = assm.GetTypes();

            if (types == null)
                return;

            TypesFound = new List<Type>();
            NamesFound = new List<string>();

            foreach (var item in types)
            {
                //is given type or its subclass
                if (item.IsSubclassOf(baseType) || item == baseType)
                {
                    TypesFound.Add(item);
                    NamesFound.Add(item.ToString());
                }
            }
        }
    }
}
